package com.xiaoxi.example.provider;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 *  简易服务提供者示例
 */
public class EasyProviderExample {

    public static void main(String[] args) throws IOException {
        // 读取yml配置文件属性值示例，用于后续功能扩展
        ObjectMapper mapper = new YAMLMapper();
        ClassLoader classLoader = EasyProviderExample.class.getClassLoader();
        InputStream inputStream = classLoader.getResourceAsStream("application.yml");
        if (inputStream != null) {
            // 读取整个YAML文件内容到Map中
            Map<String, Object> fullConfigMap = mapper.readValue(inputStream, Map.class);

            // 提取出带有' myapp.datasource'前缀的部分
            Map<String, Object> dataSourceConfig = (Map<String, Object>) fullConfigMap.get("xirpc");
            if (dataSourceConfig != null) {
                Object name = dataSourceConfig.get("name");
                Object version = dataSourceConfig.get("version");
                Object serverPort = dataSourceConfig.get("serverPort");

                System.out.println(name);
                System.out.println(version);
                System.out.println(serverPort);
            }

            inputStream.close();
        } else {
            System.out.println("无法找到资源文件application.yml");
        }
    }
}
